package com.lc.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.github.pagehelper.PageInfo;
import com.guigu.pojo.TUser;
import com.guigu.viwe.PageVo;
import com.lc.service.SysUserService;
import com.lc.vo.loginroleandshouroleView;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

@RestController
@RequestMapping("/lcsys")
public class SysUserController {


    @Resource
    private SysUserService service;

    @PostMapping("/queryall")
    public PageInfo<TUser> queryalltuser(@RequestBody PageVo<TUser> vo) {
        return this.service.queryalltuserpageService(vo);
    }

    /**
     * 添加用户
     *
     * @param user
     * @return
     */
    @PostMapping("/adduser")
    public int adduser(@RequestBody TUser user) {
        this.service.adduserService(user);
        return 1;
    }

    /**
     * 查询当前登录拥有的角色和授权账号的角色
     *
     * @param
     * @return
     */
    @GetMapping("/selectloginrole")
    public loginroleandshouroleView selectloginrole(int loginid, int shouid) {
        return this.service.selectloginroleService(loginid, shouid);
    }

    /**
     * 编辑用户
     *
     * @param user
     * @return
     */
    @PostMapping("/updateusercontroller")
    public int updateusercontroller(@RequestBody TUser user) {
        this.service.updateuserService(user);
        return 1;
    }

    @PostMapping("/authorizationusercontroller")
    public int authorizationusercontroller(@RequestBody loginroleandshouroleView view) {
        this.service.authorizationuserService(view);
        return 1;
    }

    @PostMapping("/uploadfileimgcontroller")
    public String uploadfileimgcontroller(@RequestBody() MultipartFile file) {
        String realPath = "D:\\学习资料\\S3\\三\\项目\\frontproject\\prdproject\\src\\assets";
        File savefile = new File(realPath);
        if (!savefile.exists()) {
            savefile.mkdir();
        }
        String oldname = file.getOriginalFilename();

        String newname = UUID.randomUUID() + oldname.substring(oldname.lastIndexOf("."));
        String savaPath = realPath + "\\" + newname;
        //新图片文件
        File newfile = new File(savaPath);
        //把上传存在内存中的文件内容写入目标文件中
        try {
            file.transferTo(newfile);
            System.out.println("上传成功");
            //将文件在服务器的存储路径返回
            return newname;
        } catch (IOException e) {
            System.out.println("上传失败");
            e.printStackTrace();
        }
        return null;
    }

    @PostMapping("uploadcontroller")
    public String  uploadcontroller(@RequestBody() MultipartFile myfile){
        // Endpoint以华东1（杭州）为例，其它Region请按实际情况填写。
        String endpoint = "oss-cn-hangzhou.aliyuncs.com";
        // 阿里云账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维，请登录RAM控制台创建RAM用户。
        String accessKeyId =  "LTAI5tBDtxb5zU2extkovwUW";
        String accessKeySecret = "oFZi4U5KRMtq1kZy8igFLCXcOD9l2U";
        // 填写Bucket名称，例如examplebucket。
        String bucketName = "projectvue";
        //你上传到oss后的名字 会根据日期帮你创建文件夹。
        String objectName = String.valueOf(UUID.randomUUID());
        String hz=myfile.getOriginalFilename().substring(myfile.getOriginalFilename().indexOf(".")+1);
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        try {
            InputStream inputStream = myfile.getInputStream();
            // 创建PutObject请求。
            ossClient.putObject(bucketName, objectName+"."+hz, inputStream);
        } catch (Exception oe) {

        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }

        //https://qy151.oss-cn-hangzhou.aliyuncs.com/2022/6/10/20d3d7e6b5bb455cb548675501f7270fgdnj.jpg
        String url = "https://" + bucketName + "." + endpoint + "/" + objectName+"."+hz;
        return url;
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @GetMapping("/deleteusercontroller")
    public int deleteusercontroller(int id){

        return this.service.deleteService(id);
    }


    /**
     * 查询当前登录用户
     */

    @GetMapping("/loginusercontroller")
    public TUser loginusercontroller(int id){
        return this.service.loginuserService(id);
    }

    /**
     * 修改密码验证用户和手机号
     */
    @GetMapping("/yanzshoujihaoheyhmcontroller")
    public int yanzshoujihaoheyhmcontroller(String username,String phone){
        return this.service.yanzshoujihaoheyhmService(username,phone);
    }

    @PostMapping("/xiugaimmzcontroller")
    public int xiugaimmzcontroller(@RequestBody() TUser user){
        this.service.xiugaimmzService(user);
        return 1;
    }


}
